import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;

public class main {

	/**
	 * @param args
	 * @throws ClassNotFoundException
	 */
	public static void main(String[] args) throws ClassNotFoundException {
		// TODO Auto-generated method stub
		double[][] dataInput = { { 1, 3, 5 }, { 3, 6, 656 } };
		main m = new main();
		m.writeFileText("D:\\out.txt", dataInput);
		double[][] dataOutput = m.readFileText("D:\\out.txt");
		for (int i = 0; i < dataOutput.length; i++) {
			for (int j = 0; j < dataOutput[0].length; j++)
				System.out.print(" " + dataOutput[i][j]);
			System.out.print("\n");
		}
	}

	public void writeFileBinary(String fileName, double[][] data) {
		try {
			int row = data.length;
			int col = data[0].length;
			DataOutputStream out = new DataOutputStream(
					new BufferedOutputStream(new FileOutputStream(fileName)));
			out.writeInt(row);
			out.writeInt(col);
			for (int i = 0; i < row; i++)
				for (int j = 0; j < col; j++)
					out.writeDouble(data[i][j]);
			out.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public void writeFileText(String fileName, double[][] data) {
		try {
			int row = data.length;
			int col = data[0].length;
			PrintWriter out = new PrintWriter(new BufferedWriter(
					new FileWriter(fileName)));
			out.println(String.format("%d %d", row, col));
			for (int i = 0; i < row; i++) {
				for (int j = 0; j < col; j++)
					out.print(String.format("%2.3f ", data[i][j]));
				out.println("");
			}
			out.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public void writeObjectToFile(String fileName, double[][] data) {
		try {
			ObjectOutputStream out = new ObjectOutputStream(
					new BufferedOutputStream(new FileOutputStream(fileName)));
			out.writeObject(data);
			out.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public double[][] readFileBinary(String fileName) {
		try {
			double[][] data = null;
			DataInputStream in = new DataInputStream(new BufferedInputStream(
					new FileInputStream(fileName)));
			int row = in.readInt();
			int col = in.readInt();
			data = new double[row][col];
			for (int i = 0; i < row; i++)
				for (int j = 0; j < col; j++)
					data[i][j] = in.readDouble();
			in.close();
			return data;
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return null;
	}

	public double[][] readFileText(String fileName) {
		try {
			double[][] data = null;
			BufferedReader in = new BufferedReader(new FileReader(fileName));
			String[] matrixSize = in.readLine().split(" ");
			int row = Integer.parseInt(matrixSize[0]);
			int col = Integer.parseInt(matrixSize[1]);
			data = new double[row][col];
			for (int i = 0; i < row; i++){
				String[] rowValue = in.readLine().split(" ");
				for (int j = 0; j < rowValue.length; j++)
					data[i][j] = Double.parseDouble(rowValue[j]);
			}
			in.close();
			return data;
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		return null;
	}

	public double[][] readObjectFromFile(String fileName) {
		try {
			double[][] data = null;
			ObjectInputStream in = new ObjectInputStream(
					new BufferedInputStream(new FileInputStream(fileName)));
			data = (double[][]) in.readObject();
			in.close();
			return data;
		} catch (IOException ex) {
			ex.printStackTrace();
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		}
		return null;
	}

}
